package pharmacy

class PrescriptionController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [prescriptionInstanceList: Prescription.list(params), prescriptionInstanceTotal: Prescription.count()]
    }

    def create = {
        def prescriptionInstance = new Prescription()
        prescriptionInstance.properties = params
        return [prescriptionInstance: prescriptionInstance]
    }

    def save = {
        def prescriptionInstance = new Prescription(params)
        if (prescriptionInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'prescription.label', default: 'Prescription'), prescriptionInstance.id])}"
            redirect(action: "show", id: prescriptionInstance.id)
        }
        else {
            render(view: "create", model: [prescriptionInstance: prescriptionInstance])
        }
    }

    def show = {
        def prescriptionInstance = Prescription.get(params.id)
        if (!prescriptionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'prescription.label', default: 'Prescription'), params.id])}"
            redirect(action: "list")
        }
        else {
            [prescriptionInstance: prescriptionInstance]
        }
    }

    def edit = {
        def prescriptionInstance = Prescription.get(params.id)
        if (!prescriptionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'prescription.label', default: 'Prescription'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [prescriptionInstance: prescriptionInstance]
        }
    }

    def update = {
        def prescriptionInstance = Prescription.get(params.id)
        if (prescriptionInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (prescriptionInstance.version > version) {
                    
                    prescriptionInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'prescription.label', default: 'Prescription')] as Object[], "Another user has updated this Prescription while you were editing")
                    render(view: "edit", model: [prescriptionInstance: prescriptionInstance])
                    return
                }
            }
            prescriptionInstance.properties = params
            if (!prescriptionInstance.hasErrors() && prescriptionInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'prescription.label', default: 'Prescription'), prescriptionInstance.id])}"
                redirect(action: "show", id: prescriptionInstance.id)
            }
            else {
                render(view: "edit", model: [prescriptionInstance: prescriptionInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'prescription.label', default: 'Prescription'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def prescriptionInstance = Prescription.get(params.id)
        if (prescriptionInstance) {
            try {
                prescriptionInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'prescription.label', default: 'Prescription'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'prescription.label', default: 'Prescription'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'prescription.label', default: 'Prescription'), params.id])}"
            redirect(action: "list")
        }
    }
    def sell = {PrescriptionCreatingCommand pc->
        if(pc.hasErrors()){
            return [prescription:pc];
        }else{
            def Doctor doctor = pc.doctor;
            def Patient patient= pc.patient;

            def prescriptionInstance = new Prescription();
            //prescriptionInstance.properties = params
            return [prescriptionInstance: prescriptionInstance]

        }
    }
    def drugInput = {
    }
}
class DrugInPrescriptionCommand{
    Drug drug;
    int quantity;
    String unit;
    String uses;
    static constraints={
        drug(nullable:false)
        quantity(nullable:false,min:1);
        unit(nullable:false,inList:["Tablet","Box","Bottle"])
        uses(nullable:false)
    }
}
class PrescriptionCreatingCommand{
    Doctor doctor;
    Patient patient;
    String date;
    Set<DrugInPrescriptionCommand> quantityAndUses;
    static constraints={
        /**
        doctor(nullable:false);
        patient(nullable:false);
        date(nullable:false);
        quantityAndUses(nullable:false, minLength:1);
        **/
    }
}
